﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PocoReport.Designer
{
    public partial class MainForm : Form
    {
        private Report _report;
        private Chart _selectedChart;

        public MainForm()
        {
            InitializeComponent();
        }

        private void openButton_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                _selectedChart = null;
                _report = new Report();
                using (Stream stream = openFileDialog.OpenFile())
                {
                    _report.Load(stream);
                }

                reportStructure.Nodes.Clear();
                reportStructure.Nodes.Add(new PartNode(_report.Header));
                reportStructure.Nodes.Add(new PartNode(_report.Body));
                reportStructure.Nodes.Add(new PartNode(_report.Footer));

                mainPanel.Invalidate();
            }
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                using (Stream stream = saveFileDialog.OpenFile())
                {
                    _report.Save(stream);
                }
            }
        }

        private void mainPanel_Paint(object sender, PaintEventArgs e)
        {
            if (_report != null)
            {
                ReportPrinter printer = new ReportPrinter(_report, e.Graphics);
                printer.Print(10, 10, mainPanel.Width - 20);
            }
        }

        private void mainPanel_Resize(object sender, EventArgs e)
        {
            mainPanel.Invalidate();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            PrintDocument printDocument = new PrintDocument();
            IEnumerator<int> enumerator = null;
            ReportPrinter printer = null;
            printDocument.PrintPage += delegate(object sender1, PrintPageEventArgs e1)
                                           {
                                               if (enumerator == null)
                                               {
                                                   printer = new ReportPrinter(_report, e1.Graphics);
                                                   IEnumerable<int> elements = printer.GetPages(e1.MarginBounds.Top, e1.MarginBounds.Left,
                                                                               e1.MarginBounds.Width);
                                                   enumerator = elements.GetEnumerator();
                                               }
                                               printer.Graphics = e1.Graphics;
                                               e1.HasMorePages = enumerator.MoveNext();
                                           };
            printPreviewDialog.PrintPreviewControl.Document = printDocument;
            printPreviewDialog.ShowDialog();
        }

        private class PartNode: TreeNode
        {
            public IPart Part { get; protected set; }

            public PartNode(IPart part)
            {
                Part = part;
                Text = part.GetType().Name;
                IContainerPart containerPart = part as IContainerPart;
                if(containerPart != null)
                {
                    foreach (IPart childPart in containerPart.Parts)
                    {
                        Nodes.Add(new PartNode(childPart));
                    }
                }
            }
        }

        private void reportStructure_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (reportStructure.SelectedNode != null)
            {
                propertyGrid.SelectedObject = ((PartNode)reportStructure.SelectedNode).Part;
            }
        }

        private void propertyGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            mainPanel.Invalidate();
        }
    }

    public class TestSite: ISite
    {
        public object GetService(Type serviceType)
        {
            return null;
        }

        public IComponent Component
        {
            get { throw new NotImplementedException(); }
        }

        public IContainer Container
        {
            get { return null; }
        }

        public bool DesignMode
        {
            get { return true; }
        }

        public string Name
        {
            get { return null; }
            set { throw new NotImplementedException(); }
        }
    }    
}
